﻿using System;
using System.Collections.Generic;

namespace EasyComm.Caching
{
    public class CacheState
    {
        private CacheStateItemCollection _items;
        #region public CacheStateItemCollection Items
        /// <summary>
        /// Get/Sets the Items of the CacheState
        /// </summary>
        /// <value></value>
        public CacheStateItemCollection Items
        {
            get { return _items; }
            set { _items = value; }
        }
        #endregion

        private CacheStateItemCollection _itemIdentifiers;
        #region public CacheStateItemCollection ItemIdentifiers
        /// <summary>
        /// Get/Sets the ItemIdentifiers of the CacheState
        /// </summary>
        /// <value></value>
        public CacheStateItemCollection ItemIdentifiers
        {
            get { return _itemIdentifiers; }
            set { _itemIdentifiers = value; }
        }
        #endregion

        #region public bool IsNew
        /// <summary>
        /// Gets the IsNew of the CacheState
        /// </summary>
        /// <value></value>
        public bool IsNew
        {
            get { return _items.IsNew; }
        }
        #endregion

        #region public CacheState()
        /// <summary>
        /// Initializes a new instance of the <b>CacheState</b> class.
        /// </summary>
        public CacheState()
        {
            _items = new CacheStateItemCollection();
            _itemIdentifiers = new CacheStateItemCollection();
        }
        #endregion

        #region public override bool Equals(object obj)
        /// <summary>
        /// Determines whether the specified <see cref="Object"/> is equal to the current 
        /// <b>CacheState</b>.
        /// </summary>
        /// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="UI.WebCommon.Caching.CacheState"/>.</param>
        /// <returns>true if the specified <see cref="Object"/> is equal to the current <b>CacheState</b>;
        /// otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            CacheState state = obj as CacheState;
            if (state == null)
            {
                return false;
            }

            bool equal = _items.Count == state.Items.Count;
            if (equal)
            {
                foreach (KeyValuePair<string, int> kv in _items)
                {
                    if (!state.Items.ContainsKey(kv.Key) || (state.Items.ContainsKey(kv.Key) && state.Items[kv.Key] != kv.Value))
                    {
                        equal = false;
                        break;
                    }
                }
            }
            if (equal)
            {
                foreach (KeyValuePair<string, int> kv in _itemIdentifiers)
                {
                    if (!state.ItemIdentifiers.ContainsKey(kv.Key) || (state.ItemIdentifiers.ContainsKey(kv.Key) && state.ItemIdentifiers[kv.Key] != kv.Value))
                    {
                        equal = false;
                        break;
                    }
                }
            }
            return equal;
        }
        #endregion

        #region public override int GetHashCode()
        /// <summary>
        /// Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table.
        /// </summary>
        /// <returns>A hash code for the current <see cref="UI.WebCommon.Caching.CacheState"/>.</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion

        #region public CacheState Clone()
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>A new object that is a copy of this instance.</returns>
        public CacheState Clone()
        {
            CacheState state = new CacheState();
            foreach (KeyValuePair<string, int> item in _items)
            {
                state.Items.Add(item);
            }
            foreach (KeyValuePair<string, int> item in _itemIdentifiers)
            {
                state.ItemIdentifiers.Add(item);
            }
            return state;
        }
        #endregion
    }
}
